﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace 序列化和反序列化
{
    class Program
    {
        static void Main(string[] args)
        {
            //序列化
            //peson a = new peson();
            //a.Name = "耀";
            //a.Avg = 18;
            //a.Gender = '男';
            //using(FileStream fswrite =new FileStream(@"C:\Users\Administrator\Desktop\1.txt", FileMode.OpenOrCreate, FileAccess.Write))
            //{
            //    //开始序列化对象
            //    BinaryFormatter bf = new BinaryFormatter();
            //    bf.Serialize(fswrite, a);

            //}
            //Console.WriteLine("序列化成功");
            //反序列化
            peson p;
            using(FileStream fswrite=new FileStream(@"C:\Users\Administrator\Desktop\1.txt", FileMode.OpenOrCreate, FileAccess.Read))
            {
                BinaryFormatter bf = new BinaryFormatter();
                p = (peson)bf.Deserialize(fswrite);
            }
            Console.WriteLine(p.Name);
            Console.WriteLine(p.Gender);
            Console.WriteLine(p.Avg);
            Console.ReadKey();
        }
        
    }
}
